﻿//========================================
// All rights reserved (C) 2017-2100
// CLR 版本：	4.0.30319.42000
// 命名空间:	SFJDService
// 文 件 名：	ActiveHandler
// 作    者：	WooG
// 联系方式：	wooglle@163.com
// 创建时间：	2017/10/3 17:41:30
//========================================
using Common;
using System;
using System.Collections.Generic;

namespace SFJDService
{
    public class ActiveHandler
    {
        public List<ClassActive> activeUsers = new List<ClassActive>(20);

        public ActiveHandler() { }

        /// <summary>
        /// 激活用户
        /// </summary>
        /// <param name="guid">被激活用户的Guid</param>
        /// <param name="ip">被激活用户的Ip</param>
        /// <param name="name">被激活用户的姓名</param>
        public void HanleActive(Guid guid, string ip, string name)
        {
            bool has = false;
            List<ClassActive>.Enumerator enumerator = activeUsers.GetEnumerator();
            while (enumerator.MoveNext())
            {
                if(enumerator.Current.Guid == guid)
                {
                    enumerator.Current.Ip = ip;
                    enumerator.Current.Name = name != string.Empty ? name : enumerator.Current.Name;
                    enumerator.Current.LastResponse = DateTime.Now;
                    has = true;
                    break;
                }
            }
            if (!has)
            {
                ClassActive buff = new ClassActive();
                buff.Guid = guid;
                buff.Ip = ip;
                if (name != string.Empty)
                {
                    buff.Name = name;
                }
                buff.LastResponse = DateTime.Now;
                activeUsers.Add(buff);
            }
        }
        
        public void HandlerData(Guid user_Guid, string ip, ClassData data)
        {
            bool has = false;
            List<ClassActive>.Enumerator enumerator = activeUsers.GetEnumerator();
            while (enumerator.MoveNext())
            {
                if(enumerator.Current.Guid == user_Guid)
                {
                    enumerator.Current.Data.Clone(data);
                    enumerator.Current.Ip = ip;
                    enumerator.Current.LastResponse = DateTime.Now;
                    has = true;
                    break;
                }
            }
            if (!has)
            {
                ClassActive buff = new ClassActive();
                buff.Guid = user_Guid;
                buff.Ip = string.Copy(ip);
                buff.Data.Clone(data);
                buff.LastResponse = DateTime.Now;
                activeUsers.Add(buff);
            }
        }

        public void HandlerBrigade(Guid user_Guid,string ip, long size)
        {
            bool has = false;
            List<ClassActive>.Enumerator enumerator = activeUsers.GetEnumerator();
            while (enumerator.MoveNext())
            {
                if(enumerator.Current.Guid == user_Guid)
                {
                    enumerator.Current.Ip = ip;
                    enumerator.Current.BrigadeSize = size;
                    enumerator.Current.LastResponse = DateTime.Now;
                    has = true;
                    break;
                }
            }
            if (!has)
            {
                ClassActive buff = new ClassActive();
                buff.Guid = user_Guid;
                buff.Ip = ip;
                buff.BrigadeSize = size;
                buff.LastResponse = DateTime.Now;
                activeUsers.Add(buff);
            }
        }

        public void HandlerContact(Guid user_Guid, string ip, long size)
        {
            bool has = false;
            List<ClassActive>.Enumerator enumerator = activeUsers.GetEnumerator();
            while (enumerator.MoveNext())
            {
                if (enumerator.Current.Guid == user_Guid)
                {
                    enumerator.Current.Ip = ip;
                    enumerator.Current.ContactSize = size;
                    enumerator.Current.LastResponse = DateTime.Now;
                    has = true;
                    break;
                }
            }
            if (!has)
            {
                ClassActive buff = new ClassActive();
                buff.Guid = user_Guid;
                buff.Ip = ip;
                buff.ContactSize = size;
                buff.LastResponse = DateTime.Now;
                activeUsers.Add(buff);
            }
        }

        public void HandleVersion(Guid user_Guid, string ip, Version version)
        {
            bool has = false;
            List<ClassActive>.Enumerator enumerator = activeUsers.GetEnumerator();
            while (enumerator.MoveNext())
            {
                if (enumerator.Current.Guid == user_Guid)
                {
                    enumerator.Current.Ip = ip;
                    enumerator.Current.Version = version;
                    enumerator.Current.LastResponse = DateTime.Now;
                    has = true;
                    break;
                }
            }
            if (!has)
            {
                ClassActive buff = new ClassActive();
                buff.Guid = user_Guid;
                buff.Ip = ip;
                buff.Version = version;
                buff.LastResponse = DateTime.Now;
                activeUsers.Add(buff);
            }
        }

        public void OffLine()
        {
            foreach (ClassActive active in this.activeUsers)
            {
                if (DateTime.Now.Subtract(active.LastResponse).Seconds > 10)
                {
                    this.activeUsers.Remove(active);
                }
            }
        }

        public void PrintAllUsers()
        {
            foreach(ClassActive user in activeUsers)
            {
                Runtime.Printf(user.ToString());
            }
        }
    }
}
